package com.fili.core.storage;

import com.fili.DryMain;
import com.fili.core.Log;

import javax.swing.*;
import java.util.ArrayList;
import java.io.*;

/**
 * Created by Claudiu-Florin FILIMON
 * Date: Oct 19, 2006
 * Time: 10:36:24 PM
 */
public class DataStore
{
    private ArrayList<DataItem> items;
    private ArrayList<DataStoreListener> listeners;
    private ObjectOutputStream objOutStream;
    private int ID;


    public int getID()
    {
        return ID;
    }

    public DataStore(int ID)
    {
        this(ID,null);
    }

    public DataStore(int ID, String fileName)
    {
        this.ID = ID;
        items = new ArrayList<DataItem>();
        listeners = new ArrayList<DataStoreListener>();

        boolean append = true;
        
        if ((fileName!=null)&&(fileName.trim().length()>0))
        {
            try
            {
                ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fileName));
                while (ois.available()>0)
                    items.add((DataItem) ois.readObject());
                ois.close();
            }
            catch (IOException e)
            {
                Log.writeLog(e);
                JOptionPane.showMessageDialog(DryMain.dryMainFrame, "A aparut o eroare la citirea din fisierul\n\""+fileName+"\"", "Eroare de citire",JOptionPane.ERROR_MESSAGE);
            }
            catch (ClassNotFoundException e)
            {
                Log.writeLog(e);
                JOptionPane.showMessageDialog(DryMain.dryMainFrame, "Fisierul \""+fileName+"\"\ncontine date invalide","Eroare de citire",JOptionPane.WARNING_MESSAGE);
                append = false;
            }

            try
            {
                objOutStream = new ObjectOutputStream(new FileOutputStream(fileName,append));
            }
            catch (IOException e)
            {
                Log.writeLog(e);
                JOptionPane.showMessageDialog(DryMain.dryMainFrame, "A aparut o eroare la scrierea in fisierul\n\"" + fileName + "\"", "Eroare de citire", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    public void addItem(DataItem item)
    {
        items.add(item);
        fireItemAdded(item);
        System.out.println(item);
        if (objOutStream != null)
        {
            try
            {
                objOutStream.writeObject(item);
                objOutStream.flush();
            }
            catch (IOException e)
            {
                Log.writeLog(e);
            }
        }
    }

// --------------------------
// Listener stuff

    public void addDataStoreListener(DataStoreListener listener)
    {
        if (listeners.indexOf(listener) < 0)
            listeners.add(listener);
    }

    public void removeDataStoreListener(DataStoreListener listener)
    {
        listeners.remove(listener);
    }

    protected void fireItemAdded(DataItem item)
    {
        for (DataStoreListener listener : listeners)
            listener.dataReceived(item);
    }
}
